package com.javaweb.bookstore.component;

import com.javaweb.bookstore.entity.Book;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Service//服务的提供者
@Primary
public class JdbcBookManager implements IBookManager{

    @Autowired
    ApplicationContext context;

    // 2. 私有构造函数
    private JdbcBookManager() {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl("jdbc:mysql://localhost:3306/bookstore");
            config.setUsername("root");
            config.setPassword("root");
            config.setMaximumPoolSize(20);
            config.setMinimumIdle(5);
            config.setConnectionTimeout(30000);
            config.setIdleTimeout(600000);
            config.setMaxLifetime(1800000);

            dataSource = new HikariDataSource(config);
        }catch (Exception e){

        }
    }



    HikariDataSource dataSource;
    public void createBook(Book b){


        try {
            Connection connection = dataSource.getConnection();
            String sql1 = "INSERT INTO book (id,name,description, price) VALUES (?,?,?,?)";
            // 3. 创建Statement对象
            PreparedStatement statement = connection.prepareStatement(sql1);

            statement.setString(1, b.getId());
            statement.setString(2, b.getName());
            statement.setString(3, b.getDesc());
            statement.setInt(4, b.getPrice());
            statement.executeUpdate();

            System.out.println("数据插入成功！");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 9. 关闭资源

        }
    }

    public void removeBook(String id){


    }

    public List<Book> getBooks(String key){
        try {
            Connection connection = dataSource.getConnection();
            ResultSet resultSet;
            if(key == null || key.equals("")) {

                Statement statement = connection.createStatement();

                String sql = "SELECT * FROM book";
                resultSet = statement.executeQuery(sql);
            }else{
                String sql = "SELECT * FROM book where name like ? or description like ?";
                PreparedStatement statement = connection.prepareStatement(sql);

                statement.setString(1, "%" + key + "%");
                statement.setString(2, "%" + key + "%");
                resultSet = statement.executeQuery();
            }

            System.out.println("=== 查询结果 ===");
            List<Book> books = new ArrayList<>();
            while (resultSet.next()) {
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                String desc = resultSet.getString("description");
                int price = resultSet.getInt("price");

                Book book = new Book();
                book.setId(id);
                book.setName(name);
                book.setDesc(desc);
                book.setPrice(price);
                books.add(book);


                //构造Boo对象
            }
            resultSet.close();
            return books;
        }catch (Exception e){
            return null;
        }



    }

    @Override
    public Book getBook(String id) {
        return null;
    }
}
